package com.servlet.login.root.dao.impl;

import com.servlet.login.root.bean.User;
import com.servlet.login.root.dao.UserDAO;
import com.servlet.login.root.utils.DBUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class UserDAOImpl extends DBUtils implements UserDAO {
    @Override
    public User queryByUserName(String username) {
        User user = new User();
        ResultSet queryByName = null;
        ArrayList<String> strings = new ArrayList<>();
        strings.add(username);
        queryByName = query("SELECT * FROM users WHERE username = ?;", strings);
        try {
            while (queryByName.next()){
                user.setUid(queryByName.getInt("uid"));
                user.setPassword(queryByName.getString("password"));
                user.setEmail(queryByName.getString("email"));
                user.setPhone(queryByName.getInt("phone"));
                user.setUsername(queryByName.getString("username"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeall();
        }
        return user;
    }
}
